﻿
using ComprehensiveSystem.Configuration;
using ComprehensiveSystem.IServices;
using ComprehensiveSystem.Model.Entities;
using ComprehensiveSystem.Model.FromBody;
using ComprehensiveSystem.Model.ViewModels.UI;
using ComprehensiveSystem.Utility.Extensions;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.ComponentModel;
using System.Linq.Expressions;

namespace CoreCms.Net.Web.WebApi.Controllers.SAP.Patient
{
    /// <summary>
    /// 病人信息表
    /// </summary>
    [Description("病人信息表")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PatientController : Controller
    {
        private readonly IWebHostEnvironment _webHostEnvironment;
        private readonly IPatientServices _PatientServices;

        /// <summary>
        /// 构造函数
        ///</summary>
        public PatientController(IWebHostEnvironment webHostEnvironment
            , IPatientServices PatientServices
            )
        {
            _webHostEnvironment = webHostEnvironment;
            _PatientServices = PatientServices;
        }

        #region 获取列表============================================================
        // POST: Api/Patient/GetPageList
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取列表")]
        public async Task<WebApiCallBack> GetPageList()
        {
            var jm = new WebApiCallBack();
            var pageCurrent = 1;
            var pageSize = 999999;

            //获取排序字段
            var orderField = "Id";
            //设置排序方式
            var orderDirection = "desc";
            if (Request.Form != null)
            {
                if (!Request.Form["page"].IsEmpty())
                {
                    pageCurrent = Request.Form["page"].FirstOrDefault().ObjectToInt(1);
                }
                if (!Request.Form["limit"].IsEmpty())
                {
                    pageSize = Request.Form["limit"].FirstOrDefault().ObjectToInt(30);
                }
                if (!Request.Form["orderField"].IsEmpty())
                {
                    orderField = Request.Form["orderField"].FirstOrDefault();
                }
                if (!Request.Form["orderDirection"].IsEmpty())
                {
                    orderDirection = Request.Form["orderDirection"].FirstOrDefault();
                }
            }
            Expression<Func<PatientEntity, object>> orderEx = orderField switch
            {
                "Id" => p => p.Id,
                "CreateTime" => p => p.CreateTime,
                "lastModifyTime" => p => p.LastModifyTime,
                "DeleteTime" => p => p.DeleteTime,
                "DeleteMark" => p => p.DeleteMark,
                "CreateUserId" => p => p.CreateUserId,
                "LastModifyUserId" => p => p.LastModifyUserId,
                "DeleteUserId" => p => p.DeleteUserId,
                _ => p => p.Id
            };

            //设置排序方式
            var orderBy = orderDirection switch
            {
                "asc" => OrderByType.Asc,
                "desc" => OrderByType.Desc,
                _ => OrderByType.Desc
            };
            //查询筛选
            var where = _PatientServices.ListFilter(Request);
            //获取数据
            var list = await _PatientServices.QueryPageAsync(where, orderEx, orderBy, pageCurrent, pageSize, true);
            //返回数据
            jm.status = true;
            jm.data = list;
            jm.code = 0;
            jm.data = new
            {
                result = list,
                page = list.PageIndex,
                total = list.TotalPages,
                records = list.TotalCount,
            };
            jm.msg = "数据调用成功!";
            return jm;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取所有病人")]
        public async Task<WebApiCallBack> GetList()
        {
            var jm = new WebApiCallBack();

            //获取数据
            var list = await _PatientServices.QueryListByClauseAsync(p => p.DeleteMark == 0);
            //返回数据
            jm.status = true;
            jm.data = list;

            jm.msg = "数据调用成功!";
            return jm;
        }
        #endregion

        #region 获取数据============================================================
        // POST: Api/Patient/GetEntity/10
        /// <summary>
        ///获取数据详情
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("获取数据详情")]
        public async Task<WebApiCallBack> GetEntity([FromBody] FMStringId entity)
        {
            var jm = new WebApiCallBack();

            var model = await _PatientServices.QueryByIdMarkAsync(entity.id, false);
            //if (model == null)
            //{
            //   jm.msg = "不存在此信息";
            //   return jm;
            //}
            jm.status = true;
            jm.code = 0;
            jm.data = model;

            return jm;
        }

        /// <summary>
        ///获取数据详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取数据详情")]
        public async Task<WebApiCallBack> GetDetails()
        {
            var jm = new WebApiCallBack();
            var where = _PatientServices.ListFilter(Request);
            var model = await _PatientServices.QueryByClauseAsync(where, false);
            //if (model == null)
            //{
            //   jm.msg = "不存在此信息";
            //   return jm;
            //}
            jm.status = true;
            jm.code = 0;
            jm.data = model;

            return jm;
        }
        #endregion

        #region 创建提交============================================================
        // POST: Api/Patient/DoCreate
        /// <summary>
        /// 创建提交
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("创建提交")]
        public async Task<WebApiCallBack> DoCreate([FromBody] PatientEntity entity)
        {
            var jm = await _PatientServices.InsertAsync(entity);
            return jm;
        }
        #endregion

        #region 编辑提交============================================================
        // POST: Api/Patient/Edit
        /// <summary>
        /// 编辑提交
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("编辑提交")]
        public async Task<WebApiCallBack> DoEdit([FromBody] PatientEntity entity)
        {
            var jm = await _PatientServices.UpdateAsync(entity);
            return jm;
        }
        #endregion

        #region 删除数据============================================================
        // POST: Api/Patient/DoDelete/10
        /// <summary>
        /// 单选删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("单选删除")]
        public async Task<WebApiCallBack> DoDelete([FromBody] FMStringId entity)
        {
            var jm = new WebApiCallBack();

            var model = await _PatientServices.ExistsAsync(p => p.Id == entity.id, true);
            if (!model)
            {
                jm.msg = GlobalConstVars.DataisNo;
                return jm;
            }
            jm = await _PatientServices.DeleteByIdAsync(entity.id);

            return jm;
        }

        /// <summary>
        /// 单选删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("单选删除")]
        public async Task<WebApiCallBack> DoDeleteEx([FromBody] FMStringId entity)
        {
            var jm = new WebApiCallBack();

            var model = await _PatientServices.ExistsAsync(p => p.Id == entity.id, true);
            if (!model)
            {
                jm.msg = GlobalConstVars.DataisNo;
                return jm;
            }
            jm = await _PatientServices.DeleteMarkByIdAsync(entity.id);

            return jm;
        }
        #endregion

        #region 批量删除============================================================
        // POST: Api/Patient/DoBatchDelete/10,11,20
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("批量删除")]
        public async Task<WebApiCallBack> DoBatchDelete([FromBody] FMArrayIntIds entity)
        {
            var jm = await _PatientServices.DeleteByIdsAsync(entity.Id);
            return jm;
        }

        #endregion
    }
}
